#include "TS.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
//#include <sys/time.h>


void badArgs(const char* ar)
{
    std::cout << "\nNiepoprawna skladnia" << std::endl;
    std::cout << "Sposob uzycia : " << ar << " -inputDir <dir> -inputFile <name> [-justmesh] [-forcemesh]\n\n";
	std::cout << "  gdzie \n";
	std::cout << "  <dir> - sciezka do katalogu z projektem\n";
	std::cout << "  <name> - nazwa projektu\n";
    exit(1);
}

int main(int argc, char** argv)
{

    bool justMesh = false;
    bool forceMesh = false;
    std::string inputFile;
    std::string inputDir;

    if (argc == 5)
    {
        if (!strcmp(argv[1], "-inputDir"))
            inputDir = argv[2];
        else
            badArgs(argv[0]);
        
        if (!strcmp(argv[3], "-inputFile"))
            inputFile = argv[4];
        else
            badArgs(argv[0]);
    }
    else if (argc == 6)
    {
        if (!strcmp(argv[1], "-inputDir"))
            inputDir = argv[2];
        else
            badArgs(argv[0]);
        
        if (!strcmp(argv[3], "-inputFile"))
            inputFile = argv[4];
        else
            badArgs(argv[0]);

        if (!strcmp(argv[5], "-justmesh"))
        {
            inputDir = argv[2];
            inputFile = argv[4];
            justMesh = true;
        }
        else if (!strcmp(argv[5], "-forcemesh"))
        {
            inputDir = argv[2];
            inputFile = argv[4];
            forceMesh = true;
        }
        else
            badArgs(argv[0]);
    }
    else if (argc == 7)
    {
        if (!strcmp(argv[1], "-inputDir"))
            inputDir = argv[2];
        else
            badArgs(argv[0]);
        
        if (!strcmp(argv[3], "-inputFile"))
            inputFile = argv[4];
        else
            badArgs(argv[0]);

        if (!strcmp(argv[5], "-justmesh") &&  (!strcmp(argv[6], "-forcemesh")))
        {
            inputDir = argv[2];
            inputFile = argv[4];
            justMesh = true;
            forceMesh = true;
        }
        else
            badArgs(argv[0]);
    }
    else
        badArgs(argv[0]);
    
    TS *ts = new TS();

    ts->loadInput(inputDir, inputFile);
    ts->loadZarys();

  //  timeval t1, t2;
  //  double elapsedTime;
  //  gettimeofday(&t1, NULL);

    ts->loadPodzial(forceMesh);
    
//    gettimeofday(&t2, NULL);

  //  elapsedTime = (t2.tv_sec - t1.tv_sec)*1000.0;
  //  elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
  //  std::cout << " Czas : " << elapsedTime << " ms" << std::endl;

    if (justMesh)
        return 0;

    ts->loadTemp();
    ts->saveFiles();

    delete ts;

    return 0;
}

